<template>
	<view>
		<view class="container">


			<!-- 个人信息设置区域 -->
			<view class="settings-section">
				<!-- 头像设置 -->
				<view class="setting-item" @click="handleAvatarClick">
					<view class="setting-left">
						<text class="setting-label">头像</text>
					</view>
					<view class="setting-right">
						<image class="avatar" :src="avatarUrl" mode="aspectFill"></image>
						<uni-icons type="right" size="16" color="#CCCCCC"></uni-icons>
					</view>
				</view>

				<!-- 名称设置 -->
				<view class="setting-item" @click="handleNameClick">
					<view class="setting-left">
						<text class="setting-label">名称</text>
					</view>
					<view class="setting-right">
						<text class="setting-value">{{ userName }}</text>
						<uni-icons type="right" size="16" color="#CCCCCC"></uni-icons>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		baseUrl
	} from "common/config.js";
	import {
		curl
	} from 'common/api.js';
	export default {
		data() {
			return {
				userName: '',
				avatarUrl: ''
			}
		},

		onShow() {
			var that = this;
			curl('user/index', {}).then((res) => {
				console.log(res.data);
				that.userName = res.data.nickname,
					that.avatarUrl = res.data.avatar
			});

		},
		methods: {
			handleAvatarClick() {
				var that = this;
				var token = uni.getStorageSync('token');

				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {

						var ss = res.tempFilePaths;
						
						uni.uploadFile({
							url: `${baseUrl}` + 'user/setavatar',
							filePath: ss[0], // 本地上传完成后的路径
							name: 'file',
							formData: {},
							header: {
								"token": token
							},
							success: function(uploadFileRes) {
								var ret = JSON.parse(uploadFileRes.data);
								console.log(ret.data)
								that.avatar = ret.data.avatar;
							}
						});
					}
				});
			},

			handleNameClick() {
				var that = this;

				uni.showModal({
					title: '修改名称',
					editable: true,
					placeholderText: '请输入新的名称',
					success: (res) => {
						if (res.confirm && res.content) {
							that.userName = res.content;


							curl('user/setnickname', {
								nickname: res.content
							}).then((res) => {
								console.log(res.data);

							});



						}
					}
				});
			}
		}
	}
</script>

<style>
	page {
		height: 100%;
		background-color: #F8F8F8;
	}

	.container {
		height: 100%;
	}

	.header {
		background-color: #FFFFFF;
		padding: 0 30rpx;
		border-bottom: 1px solid #EEEEEE;
	}

	.nav-bar {
		height: 88rpx;
		display: flex;
		align-items: center;
		position: relative;
	}

	.back-btn {
		width: 40px;
		height: 40px;
		display: flex;
		align-items: center;
	}

	.title {
		position: absolute;
		left: 50%;
		transform: translateX(-50%);
		font-size: 16px;
		font-weight: 500;
		color: #333333;
	}

	.settings-section {
		margin-top: 20rpx;
		background-color: #FFFFFF;
	}

	.setting-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
		border-bottom: 1px solid #EEEEEE;
	}

	.setting-item:active {
		background-color: #F5F5F5;
	}

	.setting-left {
		flex-shrink: 0;
	}

	.setting-label {
		font-size: 16px;
		color: #333333;
	}

	.setting-right {
		display: flex;
		align-items: center;
	}

	.avatar {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		margin-right: 20rpx;
	}

	.setting-value {
		font-size: 14px;
		color: #999999;
		margin-right: 20rpx;
	}
</style>